﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class HomeLayout : System.Web.UI.MasterPage
{
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand command;
    private DataSet ds;
    private string connectionString;

    protected void Page_Load(object sender, EventArgs e)
    {
        lbCount.Text = Application["visitor"].ToString();
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        connect = new SqlConnection(connectionString);
        connect.Open();
        if (Session["User"] != "")
        {
            btnLogout.Visible = true;
            lblWelcome.Visible = true;
            lblUser.Visible = true;
            lblWelcome.Text = "Welcome";
            lblUser.Text = Session["User"].ToString();
            txtUser.Visible = false;
            txtPass.Visible = false;
            btnLogin.Visible = false;
            btnRegis.Visible = false;
        }
        else
        {
            btnLogout.Visible = false;
            lblWelcome.Visible = false;
            lblUser.Visible = false;
            txtUser.Visible = true;
            txtPass.Visible = true;
            btnLogin.Visible = true;
            btnRegis.Visible = true;
            lblWelcome.Visible = true;
            lblUser.Visible = true;
            lblWelcome.Text = "You need to ";
            lblUser.Text = "Login or Register";
        }
    }
    protected void checkUser(string username, string password)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            command = new SqlCommand("spGetUser", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@username", SqlDbType.VarChar).Value = username;
            command.ExecuteNonQuery();
            da = new SqlDataAdapter(command);
            da.Fill(ds);
            if (ds.Tables[0].Rows.Count == 0)
            { 
                lblWelcome.Visible = true;
                lblUser.Visible = true;
                lblWelcome.Text = "User Name is";
                lblUser.Text = " not correct";
            }
            else if (ds.Tables[0].Rows[0][1].ToString() != password)
            {
                lblWelcome.Visible = true;
                lblUser.Visible = true;
                lblWelcome.Text = "Password is";
                lblUser.Text = " not correct";
            }
            else
            {
                btnLogout.Visible = true;
                lblWelcome.Visible = true;
                lblUser.Visible = true;
                lblWelcome.Text = "Welcome";
                lblUser.Text = username;
                Session["User"] = username;
                txtUser.Visible = false;
                txtPass.Visible = false;
                btnLogin.Visible = false;
                btnRegis.Visible = false;
            }      
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            connect.Close();
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        checkUser(txtUser.Text, txtPass.Text);
    }
    protected void btnLogout_Click(object sender, EventArgs e)
    {
        Session["User"] = "";
        btnLogout.Visible = false;
        lblWelcome.Visible = false;
        lblUser.Visible = false;
        txtUser.Visible = true;
        txtPass.Visible = true;
        btnLogin.Visible = true;
        btnRegis.Visible = true;
    }
    protected void btnRegis_Click(object sender, EventArgs e)
    {
        Response.Redirect("Register_User.aspx");
    }
}
